package com.xc.quickstart.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xc.quickstart.api.query.LoginQuery;
import com.xc.quickstart.api.query.SampleQuery;
import com.xc.quickstart.api.vo.PageVO;
import com.xc.quickstart.common.annotation.GlobalApiResponse;
import com.xc.quickstart.common.annotation.Token;
import com.xc.quickstart.common.exception.ApiArgumentBizException;
import com.xc.quickstart.common.exception.CommonBizException;
import com.xc.quickstart.service.CrmMessageService;
import com.xc.quickstart.service.dto.CrmMessageENT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/api/sample")
@Slf4j
@RestController
@Api(description = "api sample 接口", tags = "api")
@Token
@Validated
public class SampleApiController {

    @RequestMapping(value = "/hello34", method = {RequestMethod.POST, RequestMethod.GET})
    @GlobalApiResponse
    @Token(ignore = true)
    public void hello34() {
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/hello33", method = {RequestMethod.POST, RequestMethod.GET})
    @GlobalApiResponse
    @Token(ignore = true)
    public String hello33(@ApiParam("name") @RequestParam(required = false) String name) {
        if (StringUtils.isEmpty(name)) {
            throw new ApiArgumentBizException("name不能为空");
        }

        return "aaa:" + name;
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/date", method = {RequestMethod.POST, RequestMethod.GET})
    @GlobalApiResponse
    @Token(ignore = true)
    public SampleQuery date(@RequestBody SampleQuery sampleQuery) {
        return sampleQuery;
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/hello22", method = {RequestMethod.POST, RequestMethod.GET})
    @GlobalApiResponse
    @Token(ignore = true)
    public LoginQuery hello22(@ApiParam("loginVO") @RequestBody @Validated LoginQuery loginQuery) {
        return loginQuery;
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/hello", method = {RequestMethod.POST, RequestMethod.GET})
    @GlobalApiResponse
    @Token(ignore = true)
    public String hello(@ApiParam("name") @RequestParam(required = false) String name) {
        Preconditions.checkNotNull(name, "name 不能为null");
        throw new CommonBizException("aaa");
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/hello2", method = {RequestMethod.POST, RequestMethod.GET})
    @Token
    public String hello2(@ApiParam("name") @Length(min = 6) @RequestParam String name) {
        return "hello2222," + name;
    }

    @ApiOperation(value = "说hello", notes = "说hello notes")
    @RequestMapping(value = "/hello3", method = {RequestMethod.POST, RequestMethod.GET})
    @Token(ignore = true)
    public String hello3(@ApiParam("name") @RequestParam String name) {
        return "hello3333," + name;
    }


    @Autowired
    private CrmMessageService crmMessageService;

    @GlobalApiResponse
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    @Token(ignore = true)
    public PageVO<CrmMessageENT> page(@RequestBody SampleQuery sampleQuery) {
        LambdaQueryWrapper<CrmMessageENT> queryWrapper = Wrappers.<CrmMessageENT>lambdaQuery()
                .eq(CrmMessageENT::getIsValid, 1);

        IPage<CrmMessageENT> page = new Page<>(sampleQuery.getPageIndex(), sampleQuery.getPageSize(), true);
        crmMessageService.page(page, queryWrapper);

        return new PageVO<>(page);
    }
}
